<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/3/21
 * Time: 9:56
 */

namespace Home\Controller;
use Common\Common\GetsonController;
use Home\Common\Controller\CommonController;

/**
 * Class ListController
 * @package Home\Controller
 * 分类进入列表页
 */
class ListController extends CommonController
{
    //分类进来的列表
      public function lists(){
          $cid=I('get.cid');
          $bid=I('get.bid');
          if($cid){
//              面包屑导航
                $fas=$this->findfa(M('category')->select(),$cid);
                $fas[]=M('category')->where("cid=$cid")->find();
                $fas=array_reverse($fas);
                $this->assign('fas',$fas);
//              dd($fas);
              //依照该分类查除所有子类
              $soncate=$this->sonCate($cid);
              //查找拥有该商品的属性
              $son=(new GetsonController())->getsons($cid,M('category')->select());
              $son[]=$cid;
              $map['sp_goods.status']=1;
              $map['sp_goods.cid']=['in',$son];
              $goods=M('goods')
                  ->join("sp_product ON sp_goods.gid=sp_product.gid")
                  ->where($map)->field('sp_product.style')->select();
              //变成数组
              foreach($goods as $k=>$v){
                 $goods[$k]['style']=json_decode($v['style'],true);
              }
//              放到一个数组里面
              $styles=[];
              foreach($goods as  $k=>$v){
                  foreach($v['style'] as $m=>$n){
                      $styles[$m][]=$n;
                  }
              }
               $styless=[];
              foreach($styles as $k=>$v){
                  $styless[]=[
                      'name'=>$k,
                      'value'=>$v,
                  ];
              }
              //分配属性
//              dd($styless);
              $this->assign('styless',$styless);
              //处理筛选参数
              if(!isset($_GET['param'])){
                  $param=array_fill(0,count($styles),0);
              }else{
                  $param=explode('_',$_GET['param']);
              }
//              dd($_GET['param']);
//              dd($param);
              $this->assign('param',$param);
            //查找该分类下所有商品拥有的品牌
              $brand=M('goods')
                  ->join("sp_brand ON sp_goods.bid=sp_brand.bid")
                  ->where($map)->field('sp_brand.*')->group('sp_brand.bid')->select();
//              dd($brand);
              $this->assign('brand',$brand);
              //查找删选后的所有数据
              $bids=M('goods')
                  ->join("sp_brand ON sp_goods.bid=sp_brand.bid")
                  ->where($map)->field('sp_goods.bid')->group('sp_brand.bid')->select();
              $finalbids=[];
              foreach($bids as $k=>$v){
                  $finalbids[]=$v['bid'];
              }
//              dd($map);
//              dd($bids);
//              dd($finalbids);die;
              $bids=isset($_GET['bid'])?$_GET['bid']:$finalbids;
//              dd($bids);die;
              $maps['sp_goods.status']=1;
              $maps['sp_goods.cid']=['in',$son];
//              dd($bids);die;
              if(!empty($bids)){
                  $maps['sp_goods.bid']=['in',$bids];
              }
              //重组属性，依据这个在商品表中查询数据
              $findparam=[];
              $sta=0;
              foreach($param as $k=>$v){
                  if($v!='0'){
                      $findparam[$styless[$k]['name']]='%'.$styless[$k]['name'].'|'.$v.'%';
                      $sta++;
                  }
              }
              //没有属性筛选时
              if(isset($_GET['param'])){
                  if($sta!=0){
                      $maps['sp_goods.styled']=['like',$findparam,'AND'];
                  }
              }
              $finalgoods=M('goods')
                  ->join("sp_product ON sp_goods.gid=sp_product.gid")
                  ->where($maps)->group("sp_goods.gid")->select();
//              dd($finalgoods);
              $this->assign('finalgoods',$finalgoods);
          }
       $this->display();
      }

    //依照该分类查除所有子类
    public function  sonCate($cid){
        $son=(new GetsonController())->getsons($cid,M('category')->select());
        if(empty($son)){
            return M('category')->where("cid=$cid")->select();
        }else{
            $map['cid']=['in',$son];
            return M('category')->where($map)->select();
        }
    }
    //所有品牌
    private  function brand(){
        return M('brand')->select();
    }
    /**
     * 购物车
     */
    public function addcart(){
        if(IS_AJAX){
            if(!isset($_SESSION['uid'])){
                $data=['valid'=>0,'msg'=>'请登录'];
                $this->ajaxReturn($data);
            }else{
                $data=['valid'=>1,'msg'=>'已登录'];
                $this->ajaxReturn($data);
            }
        }
    }

    /**
     * 面包屑导航
     * 找当前分类的父级
     */
    public function findfa($data,$cid){
//        dd($data);
//        dd($cid);
//        die;
         static $fa=[];
        foreach($data as $k=>$v){
           if($v['cid'] == M('category')->where("cid=$cid")->getField('sid')){
              $fa[]=$v;
               $this->findfa($data,$v['cid']);
           }
        }
        return $fa;
    }
}